﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Reflection;

namespace ConsoleApplicationTest
{

    class Program
    {
        static void Main(string[] args)
        {
            var times = 1000000;
            Program program = new Program();
            object[] parameters = new object[] { new object(), new object(), new object() };
            program.Call(null, null, null); // force JIT-compile
            var watch1 = new Stopwatch();
            watch1.Start();
            for (int i = 0; i < times; i++)
            {
                program.Call(parameters[0], parameters[1], parameters[2]);
            }
            watch1.Stop();
            Console.WriteLine(watch1.Elapsed + " (Directly invoke)");

            MethodInfo methodInfo = typeof(Program).GetMethod("Call");
            var watch2 = new Stopwatch();
            watch2.Start();
            for (int i = 0; i < times; i++)
            {
                methodInfo.Invoke(program, parameters);
            }
            Console.WriteLine(watch2.Elapsed + " (Reflection invoke)");

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        public void Call(object o1, object o2, object o3) { }

        private static void NewMethod3()
        {
            string s = null;
            string se = string.Empty;
            string ses = "";

            List<string> stringEmpty = new List<string>()
            {
                null,
                string.Empty,
                ""
            };
            stringEmpty.ForEach(x =>
            {
                Console.WriteLine(x);
                Console.WriteLine("x == null: " + (x == null));
                Console.WriteLine("x == string.Empty: " + (x == string.Empty));
                Console.WriteLine("x == \"\": " + (x == ""));
                Console.WriteLine();
            });
            Console.ReadKey(true);
        }

        private static void NewMethod2()
        {
            var connString = "Data Source=.;Initial Catalog=ChinaLoveMatch.net;User ID=sa;Password=sa;";
            var mssql = new Kkbac.Tools.Datas.Old.MsSql(connString);
            mssql.ExecTransaction(x =>
            {
                var sql = "update [ChinaLoveMatch.net].dbo.[admin] set userid = '' where id < 0";
                mssql.ExecuteNonQuery(x, sql);

                sql = "update [ChinaLoveMatch.net].dbo.[admin] set userid = 'kkbacsssss' where id = 19";
                mssql.ExecuteNonQuery(x, sql);

                sql = "delete [ChinaLoveMatch.net].dbo.[admin] where id = 18";
                mssql.ExecuteNonQuery(x, sql);

                sql = "delete [ChinaLoveMatch.net].dbo.[admins] where id = 18";
                mssql.ExecuteNonQuery(x, sql);
            });

            //var sql = "update [ChinaLoveMatch.net].dbo.[admin] set userid = '' where id < 0";
            //mssql.ExecuteNonQuery(sql);

            //sql = "update [ChinaLoveMatch.net].dbo.[admin] set userid = 'kkbacsssss' where id = 19";
            //mssql.ExecuteNonQuery(sql);

            //sql = "delete [ChinaLoveMatch.net].dbo.[admin] where id = 18";
            //mssql.ExecuteNonQuery(sql);

            //sql = "delete [ChinaLoveMatch.net].dbo.[admins] where id = 18";
            //mssql.ExecuteNonQuery(sql);


            Console.ReadKey(true);
        }

        private static void NewMethod1()
        {
            var connString = "Data Source=.;Initial Catalog=ChinaLoveMatch.net;User ID=sa;Password=sa;";
            var mssql = new Kkbac.Tools.Datas.Old.MsSql(connString);

            var admin = new Admin()
            {
                id = 0,
                userid = Guid.NewGuid() + "\r\n" + Guid.NewGuid(),
                password = "p"
            };
            var s = 0;
            s = mssql.Insert(admin);
            Console.WriteLine(s);
            Console.WriteLine();

            admin = new Admin()
            {
                id = 16,
                userid = Guid.NewGuid() + "\r\n" + Guid.NewGuid(),
                password = Guid.NewGuid() + "\r\n" + Guid.NewGuid()
            };
            s = mssql.Update(admin);
            Console.WriteLine(s);
            Console.WriteLine();

            admin = new Admin()
            {
                id = 17
            };
            s = mssql.Delete(admin);
            Console.WriteLine(s);
            Console.WriteLine();
        }

        private static void NewMethod()
        {
            var connString = "Data Source=.;Initial Catalog=ChinaLoveMatch.net;User ID=sa;Password=sa;";
            var mssql = new Kkbac.Tools.Datas.Old.MsSql(connString);

            var sqls = new List<string>()
            {
                "select * from users where id < 1000",
                "select * from users where id < 0",
                "update users set username = '' where id < 0",
            };
            foreach (var sql in sqls)
            {
                var datatable = mssql.GetTable(sql);
                var listtable = mssql.GetListByTable<Users>(sql);
                var singletable = mssql.GetSingleByRow<Users>(sql);
                Console.WriteLine(sql + ", datatable == null : " + (datatable == null));
                Console.WriteLine(sql + ", listtable count: " + (listtable == null ? "null" : listtable.Count.ToString()));
                Console.WriteLine(sql + ", singletable == null : " + (singletable == null));

                var listreader = mssql.GetListByDataReader<Users>(sql);
                var singlereader = mssql.GetSingleByDataReader<Users>(sql);
                Console.WriteLine(sql + ", listreader count: " + (listreader == null ? "null" : listreader.Count.ToString()));
                Console.WriteLine(sql + ", singlereader == null : " + (singlereader == null));

                Console.WriteLine();
            }

            Console.Write("按任意键退出...");
            Console.ReadKey(true);
        }
    }
}
